package cn.mw.cmdb.service.impl;

import cn.mw.cmdb.entity.TableHeader;
import cn.mw.cmdb.mongoMapper.TableHeaderMapper;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.components.mongodb.tool.CriteriaWrapper;
import cn.mw.microMonitorCommon.api.cmdb.TableIdEnum;
import cn.mw.microMonitorCommon.security.dto.MwUser;
import cn.mw.microMonitorCommon.security.utils.SecurityUtils;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

import static cn.mw.cmdb.util.ValConvertUtil.strValueConvert;

public class InstanceTableHeaderServiceImpl extends ModelBaseServiceImpl<TableHeader, Object> {

    private CmdbServiceManage cmdbServiceManage;
    private TableHeaderMapper tableHeaderMapper;


    public static final String ID = "tableHeader";

    public InstanceTableHeaderServiceImpl(TableHeaderMapper tableHeaderMapper, CmdbServiceManage cmdbServiceManage) {
        this.tableHeaderMapper = tableHeaderMapper;
        this.cmdbServiceManage = cmdbServiceManage;
    }

    @Override
    public TableHeader genObject() {
        return new TableHeader();
    }

    @Override
    Object doAdd(TableHeader param) throws Exception {
        MwUser user = SecurityUtils.getUser();
        Long userId = user.getId();
        param.setUserId(strValueConvert(userId));
        //模型分组下不同模型字段区分，
        // TableId为customInstance和instance时，表示前端点击的是模型，此时Id是modelId
        if(TableIdEnum.customInstance.getCode().equals(param.getTableId()) ||  TableIdEnum.instance.getCode().equals(param.getTableId())){
            param.setModelId(param.getId());
        }
        tableHeaderMapper.insert(param);
        return true;
    }

    @Override
    boolean doUpdate(TableHeader param) throws Exception {
        MwUser user = SecurityUtils.getUser();
        Long userId = user.getId();
        param.setUserId(strValueConvert(userId));
        Query query = new Query(CriteriaWrapper.where(TableHeader::getUserId).is(param.getUserId()));
        // TableId为customInstance和instance时，表示前端点击的是模型，此时Id是modelId
        if(TableIdEnum.customInstance.getCode().equals(param.getTableId()) ||  TableIdEnum.instance.getCode().equals(param.getTableId())){
            query.addCriteria(new Criteria(CMDBTool.getFieldName(TableHeader::getModelId)).is(param.getId()));
        }
        query.addCriteria(new Criteria(CMDBTool.getFieldName(TableHeader::getTableId)).is(param.getTableId()));
        query.addCriteria(new Criteria(CMDBTool.getFieldName(TableHeader::getMenuId)).is(param.getMenuId()));
        //不进行id更新
        param.setId(null);
        Update update = CMDBTool.genMongoUpdate(param, null, null);
        //有数据就修改，没数据就新增
        tableHeaderMapper.upsert(query, update);
        return true;
    }

    boolean recoverFieldSetting(TableHeader param) throws Exception {
        return false;
    }

    @Override
    boolean doRemove(TableHeader param) throws Exception {
        MwUser user = SecurityUtils.getUser();
        Long userId = user.getId();
        param.setUserId(strValueConvert(userId));
        Query query = new Query(CriteriaWrapper.where(TableHeader::getUserId).is(param.getUserId()));
        query.addCriteria(new Criteria(CMDBTool.getFieldName(TableHeader::getTableId)).is(param.getTableId()));
        query.addCriteria(new Criteria(CMDBTool.getFieldName(TableHeader::getMenuId)).is(param.getMenuId()));
        tableHeaderMapper.remove(query);
        return true;
    }

    @Override
    Object doBatchAdd(Object params) throws Exception {
        return false;
    }

    @Override
    boolean doBatchUpdate(Object params) {
        return false;
    }

    @Override
    TableHeader doSelectById(Object id) throws Exception {
        return null;
    }

    @Override
    List<TableHeader> doSelectList(Object param) throws Exception {
        return null;
    }

    @Override
    boolean doBatchRemove(Object params) throws Exception {
        return false;
    }
}
